﻿//$(function () { plusReady(); })

function plusReady() {
    paintPage();
    bindEvent();
}

if (window.plus) {
    plusReady();
} else {
    document.addEventListener('plusready', plusReady, false);
}
// 银行卡列表
function paintPage() {
    $.ajax({
        type: "Get",
        url: configManager.RequstUrl + "api/common/noticelist?page=1&limit=15",
    }).done(function (data) {
        if ("success" != data.state) { console.log(data.message); return; }
        var html = [];

        for (var i = 0; i < data.data.length; i++) {
            var msg = data.data[i];

            html.push("<div name='msgitem' tip='" + msg.id + "' class='message' style='margin:8px 5px'>");
            html.push("<div class='content' style='border:1px solid #ddd;padding:0.5em;'><p style='font-size:1.2em; border-bottom:1px dashed #ddd; line-height:2em; height:2em;'><span style='font-size:1em; color:#555; float:right';>阅读<i class='angle right icon'></i></span>" + msg.title + "</p><p style='color:#777; font-size:0.9em; line-height:1.2em;'>" + msg.description + "......</p>");
            html.push("<div class='time' style='font-size:0.9em;color:#ccc; text-align:right;'>" + msg.created_at + "</div></div></div>");
        }
        if (html.length == 0) {
            $("#messagelist").html("<div style='vertical-align: middle;text-align: center;padding-top: 30%;'>暂时没有新消息</div>");
        } else {
            $("#messagelist").html(html.join(""));
        }

        try { myScroll.refresh(); } catch (err) { }
    });
}

// 消息列表
function bindEvent() {
    $("#messagelist").on("click", "div[name=msgitem]", function () {
        var id = $(this).attr("tip");
        clicked("detail.html?id=" + id, false, false, "pop-in");
    });
}

var myScroll, pullDownEl, pullDownOffset, pullUpEl, pullUpOffset;
function loaded() {
    pullDownEl = document.getElementById('pullDown');
    pullDownOffset = pullDownEl.offsetHeight;
    pullUpEl = document.getElementById('pullUp');
    pullUpOffset = pullUpEl.offsetHeight;

    myScroll = new iScroll('wrapper', {
        useTransition: false,
        topOffset: pullDownOffset,
        onRefresh: function () {
            if (pullDownEl.className.match('loading')) {
                pullDownEl.className = '';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉即可刷新';
            } else if (pullUpEl.className.match('loading')) {
                pullUpEl.className = '';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '';
            }
        },
        onScrollMove: function () {
            if (this.y > 5 && !pullDownEl.className.match('flip')) {
                pullDownEl.className = 'flip';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '松开刷新...';
                this.minScrollY = 0;
            } else if (this.y < 5 && pullDownEl.className.match('flip')) {
                pullDownEl.className = '';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉即可刷新...';
                this.minScrollY = -pullDownOffset;
            } else if (this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {
                pullUpEl.className = 'flip';
                if (isDisplayPullUp()) {
                    pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多';
                }
                this.maxScrollY = this.maxScrollY;
            } else if (this.y > (this.maxScrollY + 5) && pullUpEl.className.match('flip')) {
                pullUpEl.className = '';
                if (isDisplayPullUp()) {
                    pullUpEl.querySelector('.pullUpLabel').innerHTML = '加载中...';
                }
                this.maxScrollY = pullUpOffset;
            }
        },
        onScrollEnd: function () {
            if (pullDownEl.className.match('flip')) {
                pullDownEl.className = 'loading';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '加载中...';
                pullDownAction();
            } else if (pullUpEl.className.match('flip')) {
                pullUpEl.className = 'loading';
                if (isDisplayPullUp()) {
                    pullUpEl.querySelector('.pullUpLabel').innerHTML = '加载中...';
                }
                pullUpAction();
            }
        }
    });

    setTimeout(function () { document.getElementById('wrapper').style.left = '0'; }, 800);
}

function isDisplayPullUp() {
    // 屏幕高度 - 滚动Div面高度 - 上拉Div在滚动Div位置
    return ($(window).height() - $("#wrapper").offset().top - $("#pullUp").position().top) < 0;
}
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);

document.addEventListener('DOMContentLoaded', function () { setTimeout(loaded, 200); }, false);

// 下拉
function pullDownAction() {
    setTimeout(function () {
        paintPage();
    }, 1500);
}
// 上滑
function pullUpAction() {

}